package IO;
import java.io.*;

public class CopyBufferedStream {
	private String fromFile;
	private String toFile;

	public CopyBufferedStream(String from, String to){
		fromFile = from;
		toFile = to;
	}

	public void copy() {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(fromFile));//在源文件插读管道，外包缓冲读管道

			bos = new BufferedOutputStream(new FileOutputStream(toFile));//目标文件上插写管道，外包缓冲写管道

			byte[] buf = new byte[4096];//准备一个缓冲区数组
			int len = 0;
			while((len = bis.read(buf)) != -1){ //读来的数据保存在入参buf中，读取的实际数据长度作为返回值放在len中
				                                //当读到文件尾，无文件可读时，返回-1
				bos.write(buf, 0, len);   //把buf中的数据，从0号开始一直写到len长度，输出到目标文件       
			}

		}
                catch(IOException e) 
                   { e.printStackTrace(); }
		finally
                 {
			if(bis != null) {
				try { bis.close();}
				catch(Exception e){ e.printStackTrace(); }
			}

			if(bos !=null){
				try { bos.close(); }
				catch(Exception e){e.printStackTrace(); }
			}
		}
        }

        public static void main(String[] args){

            if(args.length != 2){
               System.out.println("Usage java FileCopy fromFile toFile");
               System.exit(0);
            }

            CopyBufferedStream fc = new CopyBufferedStream(args[0], args[1]);
            fc.copy();



        }



}